<?php

namespace app\cps\controller;

use think\facade\Request;
use app\common\Common;
use app\cps\service\OfficialAccountService;

class OfficialAccount extends Base
{
    private $service;

    public function __construct(OfficialAccountService $service)
    {
        parent::__construct();
        $this->service = $service;
    }

    // 渲染公众号设置页
    public function info()
    {
        return view();
    }

    // 公众号详情
    public function detail()
    {
        $data = $this->service->getOfficialAccountDetail($this->getUserInfo());

        if (empty($data)) {
            return Common::fail();
        }

        return Common::success($data);
    }

    // 公众号信息设置
    public function edit()
    {
        $params = Request::param();

        try {
            validate(\app\cps\validate\OfficialAccount::class)->scene('edit')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->officialAccountSetting($params, $this->getUserInfo());

        if (empty($data)) {
            return Common::fail();
        }

        return Common::success($data);
    }

    // 渲染关注回复设置页
    public function follow()
    {
        $data = $this->service->getCustomReply(1, $this->getUserInfo());

        $isCustomReply = $data ? 1 : 0;
        $followReplySwitch = $data['follow_reply_switch'];

        return view('', ['is_custom_reply' => $isCustomReply, 'follow_reply_switch' => $followReplySwitch, 'data' => $data]);
    }

    // 关注自动回复设置
    public function followReply()
    {
        $params = Request::param();

        try {
            validate(\app\cps\validate\OfficialAccount::class)->scene('follow_reply')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $params['type'] = 1;
        $data = $this->service->autoReplySetting($params, $this->getUserInfo());

        return Common::success($data);
    }

    // 渲染签到回复设置页
    public function sign()
    {
        $data = $this->service->getCustomReply(2, $this->getUserInfo());

        $isCustomReply = $data ? 1 : 0;
        $signReplySwitch = $data['sign_reply_switch'];

        return view('', ['is_custom_reply' => $isCustomReply, 'sign_reply_switch' => $signReplySwitch, 'data' => $data]);
    }

    // 签到自动回复设置
    public function signReply()
    {
        $params = Request::param();

        try {
            validate(\app\cps\validate\OfficialAccount::class)->scene('sign_reply')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $params['type'] = 2;
        $data = $this->service->autoReplySetting($params, $this->getUserInfo());

        return Common::success($data);
    }

    // 关注回复开关/签到回复开关
    public function switch()
    {
        $params = Request::param();

        try {
            validate(\app\cps\validate\OfficialAccount::class)->scene('switch')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $value = $this->service->replySwitchSetting($params, $this->getUserInfo());

        return Common::success(['value' => $value]);
    }

    // 关注/签到自动回复链接跳转下拉列表
    public function linkSelect()
    {
        $data = config('enum.reply_link_jump_url');
        return Common::success($data);
    }

    // 渲染自定义菜单页面
    public function menu()
    {
        $data = $this->service->getCustomMenuSet($this->getUserInfo());
        return view('', ['data' => $data]);
    }

    // 自定义菜单设置
    public function menuSetting()
    {
        $params = Request::param();

        try {
            validate(\app\cps\validate\OfficialAccount::class)->scene('menu_setting')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->customMenuSetting($params, $this->getUserInfo());
        if (empty($result) || !empty($result['message'])) {
            $message = !empty($result['message']) ? $result['message'] : Error::FAIL_MESSAGE;
            return Common::fail($message);
        }

        return Common::success();
    }

    // 上传素材
    public function upload()
    {
        $file = $_FILES;

        if (empty($file['file'])) {
            return Common::fail(Error::PLEASE_UPLOAD_FILE);
        }

        $data = $this->service->uploadFileToWechat($file['file'], $this->getUserInfo());

        if (!empty($data['message'])) {
            return Common::fail($data['message']);
        }
        return Common::success($data);
    }

    // 菜单设置小程序跳转路径下拉列表
    public function appletSelectUrl()
    {
        $data = config('enum.reply_link_jump_url');
        return Common::success($data);
    }

    // 公众号下拉列表
    public function select()
    {
        $params = Request::param();
        $data = $this->service->getOfficialAccountSelect($this->getUserInfo());
        return Common::success($data);
    }
}